package com.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.dao.CategoryDao;
import com.shop.entity.Buyer;
import com.shop.entity.Category;
import com.shop.entity.Result;
import com.shop.enums.CategoryStatuEnum;
import com.shop.service.CategoryService;
import com.shop.util.ResultUtil;
import com.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * description: 该文件说明
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/10/10 17:21
 **/
@Service
public class CategoryServiceImp implements CategoryService {

    @Resource
    private CategoryDao categoryDao;


    /**
     * description:<分页查找类目>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/10 17:21
     * @param  
     * @return 
     **/
    @Override
    public PageInfo<Category> findCategory(Integer page,Integer rows ,Category category) {
        PageHelper.startPage(page,rows);
        List<Category> categoryList = categoryDao.findCategory(category);
        return new PageInfo<Category>(categoryList);

    }


    /**
     * description:<添加类目>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/11 9:42
     * @param  
     * @return 
     **/
    @Override
    public Result addCategory(Category category) {
       //1.数据校验
        Result result = checkCategory(category);
        if(ResultUtil.ERROR == result.getCode()){
            return result;
        }
        //2.设置主键
        category.setCategoryId(UUIDUtil.uuid());
        //3.调用dao添加数据
        try {
            int count = categoryDao.addCategory(category);
        } catch (Exception e) {
            return ResultUtil.error("添加类目失败，请重试或者检查数据的正确性");
        }
        return ResultUtil.success("添加类目成功");
    }

    /**
     * description:<修改类目>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/11 10:24
     * @param  
     * @return 
     **/
    @Override
    public Result modifyCategory(Category category) {
        //1.数据校验
        if(StringUtils.isEmpty(category.getCategoryId())){
            return ResultUtil.error("类目id不能为空");
        }
        Result result = checkCategory(category);
        if(ResultUtil.ERROR == result.getCode()){
            return result;
        }
        //2.更新数据
        try {
            categoryDao.modifyCategory(category);
        } catch (Exception e) {
            return ResultUtil.error("更新类目失败，请检查是否数据错误");
        }
        return ResultUtil.success("更新类目成功");
    }

    /**
     * description:<上架或者下架>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/11 10:48
     * @param  
     * @return 
     **/
    @Override
    public Result updateCategoryStatu(String categoryId) {
        //1.数据校验
        if(StringUtils.isEmpty(categoryId)){
            return ResultUtil.error("类目id不能为空");
        }

        try {
            //2.通过id查找是否有相应的类目
            Category category = categoryDao.findCategoryByCategoryId(categoryId);
            if(null== category){
                return ResultUtil.error("该类目不存在，更新失败");
            }
            //3.判断是要上架还是下架
            if(CategoryStatuEnum.CATEGORY_OBTAIN.equals(category.getCategoryStatu())){
                category.setCategoryStatu(CategoryStatuEnum.CATEGORY_SHELF.getCode());
            }else{
                category.setCategoryStatu(CategoryStatuEnum.CATEGORY_OBTAIN.getCode());
            }
            //4.更新状态
            categoryDao.updateCategoryStatu(category);
        } catch (Exception e) {
            return ResultUtil.error("更新失败，请联系管理员");
        }

        return ResultUtil.success("上架/下架类目成功");
    }

    /**
     * description:<查询所有的类目>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/11 14:58
     * @param  
     * @return 
     **/
    @Override
    public List<Category> findCategoryAll() {
        List<Category> categoryList;
        try {
            categoryList = categoryDao.findCategoryAll();
        } catch (Exception e) {
            return null;
        }
        Category category = new Category();
        category.setCategoryName("全部");
        category.setCategoryType(-1);
        categoryList.add(0,category);
        return categoryList;
    }

    private Result checkCategory(Category category){
       if(StringUtils.isEmpty(category.getCategoryName())){
          return ResultUtil.error("类目名称不能为空");
       }
        if(StringUtils.isEmpty(category.getCategoryType())){
            return ResultUtil.error("类目编号不能为空");
        }
       return ResultUtil.success();

    }
}
